package gradingcalculator;

public class Term {
    
    //Attributes
    private String name;
    private String startMonth;
    private int startDay;
    private int startYear;
    private String endMonth;
    private int endDay;
    private int endYear;
    private Subject[] subjects;
    
    //Constructor
    public Term(String name, int numOfSubjects, String startMonth, int startDay, int startYear, String endMonth, int endDay, int endYear) {
        this.name = name;
        this.subjects = new Subject[numOfSubjects];
        setStartDate(startMonth, startDay, startYear);
        setEndDate(endMonth, endDay, endYear);
    }
    public Term(String name) {
        this.name = name;
        this.subjects = new Subject[0];
    }
    
    //Temporary Methods (for testing)
    public void printResult(){
        
    }
    
    //Returns
    public String getName() {
        return name;
    }
    public Subject[] getSubjects() {
        return subjects;
    }
    public Subject getSubject(int index) {
        return subjects[index];
    }
    public String getStartMonth() {
        return startMonth;
    }
    public int getStartDay() {
        return startDay;
    }
    public int getStartYear() {
        return startYear;
    }
    public String getEndMonth() {
        return endMonth;
    }
    public int getEndDay() {
        return endDay;
    }
    public int getEndYear() {
        return endYear;
    }     
    
    //Modifiers
    public final void setStartDate(String month, int day, int year){
        this.startMonth = month;
        this.startDay = day;
        this.startYear = year;
    }
    public final void setEndDate(String month, int day, int year){
        this.endMonth = month;
        this.endDay = day;
        this.endYear = year;
    }
    public void setName(String name){
        this.name = name;
    }
    
    
// ===== Advanced Modifiers (Adds, Counters, and Array Manipulators)============
    
    //ADDS
    public void addSubject(Subject subject){
        int length = this.subjects.length;
        Subject[] proxy = new Subject[length+1];
        System.arraycopy(this.subjects, 0, proxy, 0, length);
        proxy[length] = subject;
        this.subjects = proxy;
    }
    
    public void removeSubject(Subject subject, int index){
        int length = this.subjects.length;
        Subject[] proxy = new Subject[length-1];
        System.arraycopy(this.subjects, 0, proxy, 0, index);
        System.arraycopy(this.subjects, index, proxy, index, length-index);
        this.subjects = proxy;
    }
    
    public void addSubject(String name){
        //Create an element in the array if the array is null
        if(subjects == null) { subjects = new Subject[1]; }
        //Otherwise add another element to the end of array
        else { expandSubjectsBy(1); }
        //Create a Subject in the Grading Calculator named 'name parameter'
        subjects[subjects.length-1] = new Subject(name);
    }
    public void addSubject(String name, int units){
        //Create an element in the array if the array is null
        if(subjects == null) { subjects = new Subject[1]; }
        //Otherwise add another element to the end of array
        else { expandSubjectsBy(1); }
        //Create a Subject in the Grading Calculator named 'name parameter'
        subjects[subjects.length-1] = new Subject(name, units);
    }
    
    //ARRAY MANIPULATOR
    private void expandSubjectsBy(int i) {
        Subject[] proxy = new Subject[subjects.length+i];
        System.arraycopy(subjects, 0, proxy, 0, subjects.length);
        subjects = proxy;
    }
    
}